#encoding=utf-8
require 'set'
require 'net/http'
require 'mysql2'
#download photo's link in album


item_regex=/<div class="photo_wrap">(.*?color:#999.*?)<\/a>/m

photo_link_regex=/href="(.*?)" class="photolst_photo"/

photo_thumb_link=/<img src="(.*?)" \/>/

reply_count_regex=/#comments">(\d+)/


mysql_client=Mysql2::Client.new(:host => "127.0.0.1", :username => "dev",:password=>"justsoso123",:database=>"xdouban")

results=mysql_client.query("select id,album_id from album where selected=2 and id>0")

results.each do |hash|
	id=hash['id']
	album=hash['album_id']

	puts "id=#{id},album=#{album}"
	
	puts "fetch #{album}"

	html=Net::HTTP.get(URI.parse(album))
	#puts html
	html.scan(item_regex) do |item|

		#puts item

		t=[]
		
		item[0].scan(photo_link_regex){ |m| 
			puts m[0]
			t << m[0]
		}

		item[0].scan(reply_count_regex){ |m| 
			puts m[0]  
			t << m[0]
		}

		item[0].scan(photo_thumb_link){ |m|
			puts m[0]
			t << m[0]
		}

		mysql_client.query("insert into photo (album_id,album,url,reply,thumb_url) values ('#{id}','#{album}','#{t[0]}','#{t[1]}','#{t[2]}')")

	end
	
	sleep 4


end

